﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AllenData;
using AllenNeighborhood.UI;

namespace AllenNeighborhood
{
    public partial class NeighborEdit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.NeighborID = Utilities.ParseScreenInt(this.Request.QueryString["NeighborID"]);

            if (!this.IsPostBack)
            {
                this.LoadInteractionTypes();
                this.LoadInteractionModes();
                this.LoadTopics();

                this.InteractionDateTextBox.Text = DateTime.Now.ToString();
            }            
        }

        public int NeighborID
        {
            get;
            set;
        }

        protected void LoadInteractionModes()
        {
            var data = new InteractionModeData();
            this.InteractionModeDropDown.DataSource = data.GetAllInteractionModes();
            this.InteractionModeDropDown.DataTextField = "Name";
            this.InteractionModeDropDown.DataValueField = "InteractionModeID";
            this.InteractionModeDropDown.DataBind();
        }

        protected void LoadInteractionTypes()
        {
            var data = new InteractionTypeData();
            this.InteractionTypeDropDown.DataSource = data.GetAllInteractionTypes();
            this.InteractionTypeDropDown.DataTextField = "Name";
            this.InteractionTypeDropDown.DataValueField = "InteractionTypeID";
            this.InteractionTypeDropDown.DataBind();
        }

        protected void LoadTopics()
        {
            var data = new TopicData();
            this.TopicsRepeater.DataSource = data.GetAllTopics();
            this.TopicsRepeater.DataBind();
        }

        protected void TopicsRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                HiddenField field = (HiddenField)e.Item.FindControl("InteractionTopicID");
                field.Value = ((Topic)e.Item.DataItem).TopicID.ToString();
            }
        }

        protected void SaveButton_Click(object sender, EventArgs e)
        {
            this.SaveForm();
        }

        protected void SaveForm()
        {
            Interaction interaction = new Interaction();
            interaction.NeighborID = this.NeighborID;
            interaction.InteractionTypeID = Utilities.ParseScreenInt(this.InteractionTypeDropDown.SelectedValue);
            interaction.InteractionModeID = Utilities.ParseScreenInt(this.InteractionModeDropDown.SelectedValue);
            interaction.InteractionDate = Utilities.ParseScreenDate(this.InteractionDateTextBox.Text).GetValueOrDefault();
            interaction.Note = this.NoteTextBox.Text;

            var data = new InteractionData();
            int interactionID = data.AddNewInteraction(interaction);

            foreach (RepeaterItem item in this.TopicsRepeater.Items)
            {
                InteractionTopic interactionTopic = null;
                CheckBox FollowUpCheckBox = (CheckBox)item.FindControl("FollowUpCheckBox");
                if (FollowUpCheckBox.Checked)
                {
                    interactionTopic = new InteractionTopic();
                    interactionTopic.Followup = true;
                }
                else
                {
                    CheckBox InfoCheckBox = (CheckBox)item.FindControl("InfoCheckBox");
                    if (InfoCheckBox.Checked)
                    {
                        interactionTopic = new InteractionTopic();
                    }
                }

                if (interactionTopic != null)
                {
                    interactionTopic.InteractionID = interactionID;
                    interactionTopic.TopicID = Utilities.ParseScreenInt(((HiddenField)item.FindControl("InteractionTopicID")).Value);

                    var tData = new InteractionTopicData();
                    tData.AddNewInteractionTopicData(interactionTopic);
                }
            }

            NeighborData nd = new NeighborData();
            Neighbor neighbor = nd.GetNeighbor(this.NeighborID);

            this.Response.Redirect(string.Format("~/Secure/NeighborList.aspx?StreetNumber={0}&StreetID={1}", neighbor.StreetNumber, neighbor.StreetID));
        }
    }
}
